<?php /* Smarty version 2.6.20, created on 2013-05-25 01:50:46
         compiled from _core/_include/module/_mvc/control/handledialogpurchasenote.tpl */ ?>
function HandleDialogPurchaseNote(gui, item, options) {
  this._gui = gui;
  this._item = item;
  this._options = options;
  this._popupBox = new PopupBox();
  
  this._author = (options && options.author) ? options.author : null;
  this._now = (options && options.now) ? options.now : null;
  this._popupBox2 = (options && options.popupBox) ? options.popupBox : null;
  this._callbackFunc = (options && options.callbackFunc) ? options.callbackFunc : null;
  this._parent = (options && options.parent) ? options.parent : null;
  
  this._createElements();
};

HandleDialogPurchaseNote.prototype._createElements = function() {
  var tr, td;
  tr = this._gui.mains.insertRow(-1);
  td = tr.insertCell(-1);
  td.colSpan = 4;
  td.style.borderRight = 'solid 1px #b4b4b4';
  td.style.borderBottom = 'solid 1px #b4b4b4';
  td.style.textAlign = 'center';
  td.appendChild(DOMUtils.getLoadingImage());
  
  this._loadData();
};

HandleDialogPurchaseNote.prototype._loadData = function() {
  this._purchase = null;
  this._retrievePurchase();
};

HandleDialogPurchaseNote.prototype._verifyData = function() {
  if (this._purchase) {
    this._updateElements();
  }
};

HandleDialogPurchaseNote.prototype._retrievePurchase = function(pos) {
  var _self = this;
  /*new RequestUtils()._read('purchaseitem', null, 'd.oid =' + this._item.id , null, null, null, null, function(result, params) { _self._purchase = result.data[0];
                                                                                                                                _self._verifyData.call(_self);
                                                                                                                              }, {pos: (pos) ? pos : null});
*/

  var args = '&oid=' + this._item.id; 
  new RequestUtils()._mysql('v1purchaseitem', args, function(result, params) { _self._purchase = result.data[0];
                                                                          _self._verifyData.call(_self);
                                                                        }, {php:'customphp/mysqlread.php'});    
};

HandleDialogPurchaseNote.prototype._updateElements = function() {
  var _self = this;
  DOMUtils.removeTableRows(this._gui.mains, 1);
  
  if (this._purchase.notes.length == 0) {
    tr = this._gui.mains.insertRow(-1);
    td = tr.insertCell(-1);
    td.colSpan = 4;
    td.style.height = '24px';
    td.style.textAlign = 'center';
    td.style.borderRight = 'solid 1px #b4b4b4';
    td.style.borderBottom = 'solid 1px #b4b4b4';
    td.appendChild(document.createTextNode('N/A'));
  }
  
  MiscUtils.sort(this._purchase.notes, ['content']);
  
  for (var i = 0, il = this._purchase.notes.length; i < il; i++) {
    var item = this._purchase.notes[i];
    tr = this._gui.mains.insertRow(-1);
    td = tr.insertCell(-1);
    if (item.cancelled == 1) {
      td.style.textDecoration = 'line-through';
    }
    td.style.height = '24px';
    td.style.textAlign = 'center';
    td.style.borderRight = 'solid 1px #b4b4b4';
    td.style.borderBottom = 'solid 1px #b4b4b4';
    td.appendChild(document.createTextNode(SimpleDate.format(item.date)));
    
    td = tr.insertCell(-1);
    if (item.cancelled == 1) {
      td.style.textDecoration = 'line-through';
    }
    td.style.textAlign = 'left';
    td.style.padding = '0 0 0 15px';
    td.style.borderRight = 'solid 1px #b4b4b4';
    td.style.borderBottom = 'solid 1px #b4b4b4';
    td.appendChild(document.createTextNode(item.author));
    
    td = tr.insertCell(-1);
    if (item.cancelled == 1) {
      td.style.textDecoration = 'line-through';
    }
    td.style.textAlign = 'left';
    td.style.padding = '0 0 0 15px';
    td.style.borderRight = 'solid 1px #b4b4b4';
    td.style.borderBottom = 'solid 1px #b4b4b4';
    if (item.content.length != 0) {
      td.appendChild(document.createTextNode(item.content));
    } else {
      td.appendChild(document.createTextNode('(none)'));
    }
    
    td = tr.insertCell(-1);
    td.style.textAlign = 'center';
    td.style.borderRight = 'solid 1px #b4b4b4';
    td.style.borderBottom = 'solid 1px #b4b4b4';
    var remove = document.createElement('img');
    remove.src = 'images/common/delete.png';
    remove.style.cursor = 'pointer';
    remove._object = item;
    remove.onclick = function() { this._object.cancelled = 1;
                                  var pos = DOMUtils.findPos(this);
                                  new RequestUtils()._write('v1purchasenote', [this._object], [], function() { _self._retrievePurchase(pos); }, { php: Constant.PATH + '/customphp/mysqlwrite.php', pos: pos });
                                };
    if (item.cancelled == 0 && this._item.cancelled == 0 && (this._parent.disposalstatus == 0)) {
      td.appendChild(remove);
    }
  }
  
  this._gui.input.disabled = false;
  if (this._item.cancelled == 1 || (this._parent.disposalstatus == 1)) {
    this._gui.input.disabled = true;
    this._gui.add.style.display = 'none';
  }
  
  this._note = PurchaseNote.instance();
  this._gui.input.value = this._note.content;
  this._gui.input.onchange = function() { _self._note.content = this.value; };
  
  this._gui.add.onclick = function() { if (_self._note.content == '') {
                                         window.alert('CAN NOT BE EMPTY');
                                         return;
                                       }
                                       _self._note.author = _self._author.account;
                                       _self._note.date = _self._now;
                                       _self._purchase.notes[_self._purchase.notes.length] = _self._note;
                                       var pos = DOMUtils.findPos(this);
                                       new RequestUtils()._write('purchaseitem', [_self._purchase], [], function() { _self._retrievePurchase(pos); }, { php: Constant.PATH + '/customphp/mysqlwrite.php', pos: pos });
                                     };
  this._gui.close.onclick = function() { _self._popupBox2._close(); _self._callbackFunc(); };
};
